package com.yunsom.testdemo.multiple_thread.concurrent_tools;

import java.util.concurrent.*;

public class ExecutorTest {
    public static void main(String[] args) throws InterruptedException {

        System.out.println("==缓存线程的线程池");
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            cachedThreadPool.execute(new MyRunnable());
        }
        cachedThreadPool.shutdown();

        Thread.sleep(2000);

        System.out.println("==固定长度的线程池");
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 5; i++) {
            fixedThreadPool.execute(new MyRunnable());
        }
        fixedThreadPool.shutdown();

        Thread.sleep(1000);
        System.out.println("==定时线程池");
        ScheduledExecutorService scheduledThreadPool= Executors.newScheduledThreadPool(2);
        for (int i = 0; i < 5; i++) {
            // 3s后执行
            scheduledThreadPool.schedule(new MyRunnable(), 3, TimeUnit.SECONDS);
        }
        scheduledThreadPool.shutdown();

        Thread.sleep(1000);
        System.out.println("==单一线程池");
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 5; i++) {
            singleThreadPool.execute(new MyRunnable());
        }
        singleThreadPool.shutdown();

    }
}
